package com.enterpriseDigitalPlatform.controller.renshi.staff;

import com.enterpriseDigitalPlatform.dto.JsonRes;
import com.enterpriseDigitalPlatform.dto.PageRes;
import com.enterpriseDigitalPlatform.exception.ServiceException;
import com.enterpriseDigitalPlatform.util.RequiredCheckUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/renshi-staff")
public class StaffC {
    private StaffDao staffDao;

    @Autowired
    public StaffC(StaffDao staffDao) {
        this.staffDao = staffDao;
    }

    // 登录账号生成
    private String makeAccount(String cardid) {
        if (cardid.length() > 6) {
            return cardid.substring(cardid.length() - 6);
        }
        StringBuilder sb = new StringBuilder();
        // 计算需要填充的'0'的数量
        int paddingLength = 6 - cardid.length();
        // 填充'0'
        sb.append("0".repeat(paddingLength));
        // 添加原字符串
        sb.append(cardid);
        return sb.toString();
    }

    // 校验是否为黑名单
    private boolean testIsBlack(String cardid, Integer id) {
        StaffDTO staffDTO = new StaffDTO();
        staffDTO.setCardid(cardid);
        staffDTO.setId(id);
        PageRes<Staff> pageRes = staffDao.getList(staffDTO);
        List<Staff> list = pageRes.getData();
        if (!list.isEmpty()) {
            Staff staff = list.get(0);
            return staff.getIsBlack() == 1;
        }
        return false;
    }

    // 新加员工信息
    @PostMapping("/addData")
    public Object addData(@RequestBody StaffDTO staffDTO) {
        if (RequiredCheckUtil.checkString(staffDTO.getCardid())) {
            throw new ServiceException("cardid不能为空");
        }
        if (RequiredCheckUtil.checkString(staffDTO.getAccount())) {
            staffDTO.setAccount(makeAccount(staffDTO.getCardid()));
        }
        if (RequiredCheckUtil.checkString(staffDTO.getPwd())) {
            staffDTO.setPwd("123456");
        }
        boolean isBlack = testIsBlack(staffDTO.getCardid(), null);
        if (isBlack) {
            throw new ServiceException("该人员在黑名单中,禁止操作");
        }
        int row = staffDao.addData(staffDTO);
        return new JsonRes<>(row);
    }

    // 编辑员工信息
    @PostMapping("/editData")
    public Object editData(@RequestBody StaffDTO staffDTO) {
        if (RequiredCheckUtil.checkInt(staffDTO.getId())) {
            throw new ServiceException("id不能为空");
        }
        boolean isBlack = testIsBlack(null, staffDTO.getId());
        if (isBlack) {
            throw new ServiceException("该人员在黑名单中,禁止操作");
        }
        int row = staffDao.editData(staffDTO);
        return new JsonRes<>(row);
    }

    // 黑名单修改
    @PostMapping("/editIsBlack")
    public Object editIsBlack(@RequestBody StaffDTO staffDTO) {
        if (RequiredCheckUtil.checkInt(staffDTO.getId())) {
            throw new ServiceException("id不能为空");
        }
        if (staffDTO.getIsBlack() == null) {
            throw new ServiceException("黑名单参数不能为空");
        }
        int row = staffDao.editBlack(staffDTO);
        return new JsonRes<>(row);
    }

    // 重置密码
    @PostMapping("/resetPwd")
    public Object resetPwd(@RequestBody StaffDTO staffDTO) {
        if (RequiredCheckUtil.checkInt(staffDTO.getId())) {
            throw new ServiceException("id不能为空");
        }
        boolean isBlack = testIsBlack(null, staffDTO.getId());
        if (isBlack) {
            throw new ServiceException("该人员在黑名单中,禁止操作");
        }
        staffDTO.setPwd("123456");
        int row = staffDao.editPwd(staffDTO);
        return new JsonRes<>(row);
    }

    // 修改密码
    @PostMapping("/editPwd")
    public Object editPwd(@RequestBody StaffDTO staffDTO, HttpServletRequest request) {
        if (RequiredCheckUtil.checkString(staffDTO.getPwd())) {
            throw new ServiceException("密码不能为空");
        }
        Map<String, Object> jwtData = (Map<String, Object>) request.getAttribute("jwtData");
        staffDTO.setId((Integer) jwtData.get("id"));
        int row = staffDao.editPwd(staffDTO);
        return new JsonRes<>(row);
    }

    // 获取员工列表
    @PostMapping("/getList")
    public Object getList(@RequestBody StaffDTO staffDTO) {
        if (RequiredCheckUtil.checkInt(staffDTO.getPage())) {
            staffDTO.setPage(1);
        }
        if (RequiredCheckUtil.checkInt(staffDTO.getPageSize())) {
            staffDTO.setPageSize(20);
        }
        PageRes<Staff> pageRes = staffDao.getList(staffDTO);
        pageRes.getData().forEach(staff -> {
            staff.setPwd(null);
            staff.setToken(null);
        });
        return new JsonRes<>(pageRes);
    }

    // 获取员工
    @PostMapping("/getStaff")
    public Object getStaff(@RequestBody StaffDTO staffDTO) {
        if (RequiredCheckUtil.checkInt(staffDTO.getId())) {
            throw new ServiceException("id不能为空");
        }
        PageRes<Staff> pageRes = staffDao.getList(staffDTO);
        List<Staff> list = pageRes.getData();
        Staff staff = new Staff();
        if (!list.isEmpty()) {
            staff = list.get(0);
            staff.setPwd(null);
            staff.setToken(null);
        }
        return new JsonRes<>(staff);
    }
}
